package tds.model22;

import java.util.concurrent.CountDownLatch;

/**
 * @Company 杭州先锋电子技术股份有限公司
 * @ClassName Client
 * @Description 测试多线程下不同方式的单例模式的效率
 * @author tds
 * @Date 2020/2/5 22:40
 * @version 1.0.0
 */
public class Client4 {
    public static void main(String[] args) throws Exception {
        // 线程数
        int threadNum  = 10;

        // 一个计数器，这里记录线程数
        final CountDownLatch countDownLatch = new CountDownLatch(threadNum);

        long start = System.currentTimeMillis();

        for (int i = 0; i < threadNum; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000000; i++){
                        Singleton1.getInstance();
                    }
                    countDownLatch.countDown();
                }
            }).start();
        }

        // 等待计数器为0，即线程跑完了
        countDownLatch.await();

        long end = System.currentTimeMillis();

        System.out.println("本次测试耗时：" + (end - start) + "ms");
    }
}
